var map;
var geocoder;
var jsonStr;
	
function initialize() {
	jQuery("#discover-button").click(function() {		
		updateMap();
	});
    var latlng = new google.maps.LatLng(19.973348786110602, -2.4609375);
    var myOptions = {
      zoom: 2,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);
}

function updateMap() {
	geocoder = new google.maps.Geocoder();
	var address = document.getElementById("keywords").value;
	// CALL SERVER TO GET JSON
	jsonStr =  [{"Title":"Title24","Summary":"Summary22","Link":"link1","Rank":99,"Locations":["sterling","washington dc"],"Topics":null,"PublishedDate":"\/Date(1319346805250)\/"},{"Title":"Title24","Summary":"Summary22","Link":"link1","Rank":99,"Locations":["fairfax","washington dc"],"Topics":null,"PublishedDate":"\/Date(1319346805250)\/"}];	
	createMarkers();
}

function createMarkers(){
	var locSizeArray = new Object();
	$.each(jsonStr, function(i,item){
		var locc;
		$.each(item.Locations, function(j,loc){	
			if (locSizeArray[loc] == null) {
				locSizeArray[loc] = 1;
			} else {
				locSizeArray[loc]++;
			}
			locc = loc;			
		});
    });
    for (var key in locSizeArray) {
    	createMarker(key, locSizeArray[key]);
	}
}

function createMarker(address,r) {
	  geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        map.setCenter(results[0].geometry.location);
        var marker = new google.maps.Marker({
            position: results[0].geometry.location
        });
        var circle = new google.maps.Circle({
          map: map,
          radius: r * 5000
        });
        circle.bindTo('center', marker, 'position');
      } else {
        alert("Geocode was not successful for the following reason: " + status);
      }
    });
}

